package com.ymatou.iapi.email.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.mongodb.util.Hash;
import com.ymatou.iapi.email.parameter.EmailCallBackBean;
import com.ymatou.iapi.email.parameter.SendEmailBean;
import com.ymatou.iapi.email.service.EmailCallBackCall;
import com.ymatou.iapi.email.service.SendEmailCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.MailContent;
import com.ymttest.database.model.MailRecord;
import com.ymttest.database.sqlwapper.EmailWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 发送邮件 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_EmailCallBack {
	private static SendEmailBean sendemailBean;
	private static SendEmailCall sendemailCall;
	private static EmailCallBackBean ecallbackbean;
	private static EmailCallBackCall ecallbackcall;
	private static EmailWapper emailwapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送邮件");
		emailwapper = new EmailWapper();
	}

	@Before
	public void caseUp() {
		sendemailBean = new SendEmailBean();
		sendemailCall = new SendEmailCall();
		ecallbackbean=new EmailCallBackBean();
		ecallbackcall=new EmailCallBackCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendemailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_EmailCallBack_001() {
		Logger.start(true, "发送邮件成功");
		try {
			sendemailBean.setAppId("AutoTest");
			sendemailBean.setContent("测试");
			// 测试账号 配置在mail_sender 表中
			sendemailBean.setFromMail("alarm@ymatou.com");
			sendemailBean.setFromName("邮件测试AutoTest");
			sendemailBean.setSubject("subject");
			sendemailBean.setToMail("zhouyi@ymatou.com");
			sendemailCall.setData(sendemailBean);
			sendemailCall.callService();
			Logger.verifyEquals("success", sendemailCall.getString("errorMessage"), "验证errorMessage");
			Logger.verifyEquals("true", sendemailCall.getString("success"), "验证success");
			Logger.verifyEquals("0", sendemailCall.getString("errorCode"), "验证errorCode");

			JsonArray idList = sendemailCall.getJsonObject().getAsJsonObject("result").getAsJsonArray("idList");
			String rid=idList.get(0).getAsString();
			
			ecallbackbean.setMailRecordId(rid);
			ecallbackcall.setData(ecallbackbean);
			ecallbackcall.callService();
			Logger.verifyEquals("ok", ecallbackcall.getReturnData(), "验证ReturnData");
			MailRecord record = emailwapper.selectMailRecordById(rid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(record));
			Logger.verifyEquals(true, record.getStatus() == 1, "验证Status为1");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}